/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package projecteuler.problems.dates19;

/**
 *
 * @author CJ
 */
public class Dates {

    public static void run(){
        Date currentDate = new Date(6, Month.JANUARY, 1901);
        Date finalDate = new Date(24, Month.DECEMBER, 2000);
        int current = 0;
        System.out.println(currentDate);
        while(!currentDate.equals(finalDate)){
            currentDate.addDays(7);
            if(currentDate.day == 1){
                current++;
            }
            System.out.println(currentDate);
        }
        System.out.println(current);
    }

}
class Date{
    int day;
    Month month;
    int year;

    public Date(int day, Month month, int year){
        this.day = day;
        this.month = month;
        this.year = year;
    }

    public void addDays(int daysToAdd){
        this.day += daysToAdd;
        normalize();
    }

    private void checkDay(int max){
        if(this.day > max){
            this.day -= max;
            incrementMonth();
        }
    }

    private void normalize(){
        if (this.month == Month.SEPTEMBER ||
                this.month == Month.APRIL ||
                this.month == Month.JUNE ||
                this.month == Month.NOVEMBER){
                checkDay(30);
        }else if(this.month == Month.FEBRUARY){
                if(this.year % 4 == 0){
                    if(this.year % 100 == 0){
                        if(this.year % 400 == 0){
                            checkDay(29);
                        }else{
                            checkDay(28);
                        }
                    }else{
                        checkDay(29);
                    }
                }else{
                    checkDay(28);
                }
        }else{
            checkDay(31);
        }
    }

    private void incrementMonth(){
        System.out.println("called");
        Month[] months = Month.values();
        for(int i = 0; i < months.length; ++i){
            if(this.month == months[i]){
                if(i == months.length - 1){
                    this.month = months[0];
                    this.year++;
                }else{
                    this.month = months[i + 1];
                }
                break;
            }
        }
    }

    public boolean equals(Object obj){
        if(obj instanceof Date){
            Date toCompare = (Date)obj;
            if(toCompare.day == this.day && toCompare.month == this.month
                    && toCompare.year == this.year){
                return true;
            }
        }
        return false;
    }

    public String toString(){
        return String.format("Day: %1$d | Month: %2$s | Year: %3$d",
                this.day, this.month, this.year);
    }
}
enum Day {
    SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
        THURSDAY, FRIDAY, SATURDAY
}
enum Month {
    JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE, JULY, AUGUST, SEPTEMBER,
    OCTOBER, NOVEMBER, DECEMBER
}